package com.dwy2002.product.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dwy2002.common.Result;
import com.dwy2002.common.utils.Validator;
import com.dwy2002.enums.Status;
import com.dwy2002.pojo.dto.product.CategoriesDTO;
import com.dwy2002.pojo.dto.product.CategoriesPageDTO;
import com.dwy2002.pojo.po.product.Categories;
import com.dwy2002.pojo.vo.product.CategoriesVO;
import com.dwy2002.product.mapper.CategoriesMapper;
import com.dwy2002.product.service.ICategoriesService;
import com.dwy2002.product.service.IProductCategoryService;
import com.dwy2002.product.service.IProductsService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.dwy2002.common.Result.*;
import static com.dwy2002.common.utils.Utils.*;

/**
 * @author 杜伟毅
 * @version 1.0
 * @since 2025/02/17
 */
@Service
@RequiredArgsConstructor
public class CategoriesServiceImpl extends ServiceImpl<CategoriesMapper, Categories> implements ICategoriesService {
    private final IProductsService productsService;
    private final IProductCategoryService productCategoryService;

    /**
     * 添加分类
     *
     * @param dto 商品分类信息接收类
     * @author 杜伟毅
     * @since 2025/3/1 0001 15:18
     */
    @Override
    public Result<Void> saveCategories(CategoriesDTO dto) {
        if (isEmptyOrNull(dto.getName())) return fail(Status.BAD_REQUEST, "分类名称不能为空");
        boolean save = save(BeanUtil.copyProperties(dto, Categories.class));
        return saveResult(save, "保存成功", "保存失败");
    }

    /**
     * 修改分类
     *
     * @param dto 商品分类信息接收类
     * @author 杜伟毅
     * @since 2025/3/1 0001 16:10
     */
    @Override
    public Result<Void> updateCategories(CategoriesDTO dto) {
        Validator.of(dto)
                .notEmpty(CategoriesDTO::getName, "id不能为空")
                .notEmpty(CategoriesDTO::getName, "名称不能为空")
                .validate();

        LambdaUpdateWrapper<Categories> luw = new LambdaUpdateWrapper<>();
        luw.eq(Categories::getId, dto.getId())
                .set(Categories::getName, dto.getName());
        boolean update = update(luw);
        return handleResult(update, "修改成功", "修改失败");
    }

    /**
     * 获取分类分页数据
     *
     * @param dto 分类分页前端接受类
     * @author 杜伟毅
     * @since 2025/3/1 0001 22:23
     */
    @Override
    public Result<Page<Categories>> getCategoriesPage(CategoriesPageDTO dto) {
        Validator.of(dto)
                .mustSatisfy(isPageNull(dto.getPageNo(), dto.getPageSize()), "请传入分页数据")
                .mustSatisfy(isPageZero(dto.getPageNo(), dto.getPageSize()), "分页数据不能为0")
                .validate();

        LambdaQueryWrapper<Categories> lqw = new LambdaQueryWrapper<>();
        lqw.like(Categories::getName, dto.getName())
                .orderBy(dto.getCreatedEnabled(), dto.getCreatedIsAsc(), Categories::getCreatedAt)
                .orderBy(dto.getUpdatedEnabled(), dto.getUpdatedIsAsc(), Categories::getUpdatedAt);
        Page<Categories> page = new Page<>(dto.getPageNo(), dto.getPageSize());
        Page<Categories> categoriePage = this.page(page, lqw);
        return success(categoriePage);
    }

    /**
     * 删除分类
     *
     * @param id 分类id
     * @author 杜伟毅
     * @since 2025/3/2 0002 11:18
     */
    @Override
    public Result<Void> deleteCategories(String id) {
        if (isEmptyOrNull(id)) return fail(Status.BAD_REQUEST, "id不能为空");
        //根据分类id获取从商品分类关系表中获取商品id集合
        List<String> productIds = productCategoryService.getProductIdByCategoryId(id);
        //根据商品id集合删除商品
        String productsMessage = productsService.deleteListProduct(productIds).getMessage();
        //删除商品分类关系
        productCategoryService.deleteProductToCategory(id);
        //删除分类
        boolean remove = removeById(id);
        return handleResult(remove, "删除成功,商品" + productsMessage, "删除失败,商品" + productsMessage);
    }

    /**
     * 获取所有分类名字
     *
     * @author 杜伟毅
     * @since 2025/5/9 0009 13:35
     */
    @Override
    public Result<List<CategoriesVO>> getAllCategoriesName() {
        return success(BeanUtil.copyToList(list(new LambdaQueryWrapper<>()), CategoriesVO.class));
    }

    /**
     * 获取分类详情
     *
     * @param id 分类id
     * @author 杜伟毅
     * @since 2025/5/10 0010 13:31
     */
    @Override
    public Result<Categories> getCategoriesDetails(String id) {
        return success(getById(id));
    }

}
